<?php
/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Album
 * @copyright  Copyright 2006-2010 Webligo Developments
 * @license    http://www.socialengine.net/license/
 * @version    $Id: Core.php 9071 2011-07-20 23:43:30Z john $
 * @author     Sami
 */

/**
 * @category   Application_Extensions
 * @package    Album
 * @copyright  Copyright 2006-2010 Webligo Developments
 * @license    http://www.socialengine.net/license/
 */
class Ialbum_Api_Core extends Core_Api_Abstract
{
  public function getPeopleInAlbum($album_id) {
    $userTable = Engine_Api::_()->getDbtable('Users', 'User');
    $userTableName = $userTable->info('name');
    $albumTable = Engine_Api::_()->getDbtable('Albums', 'Album');
    $albumTableName = $albumTable->info('name');
    $photoTable = Engine_Api::_()->getDbtable('Photos', 'Album');
    $photoTableName = $photoTable->info('name');
    $tagmapsTable = Engine_Api::_()->getDbtable('TagMaps', 'Core');
    $tagmapsTableName = $tagmapsTable->info('name');
    
    $select = $userTable->select()
    					->setIntegrityCheck(false)
                    	->from($userTableName, array())
                    	->join($tagmapsTableName, $tagmapsTableName . '.tag_id=' . $userTableName . '.user_id', array($userTableName . '.*'))
                    	->join($photoTableName, $tagmapsTableName . '.resource_id=' . $photoTableName . '.photo_id', array())
  						->join($albumTableName, $photoTableName . '.album_id=' . $albumTableName . '.album_id', array())
  						->where($albumTableName . '.album_id = ?', $album_id)
			            ->where($tagmapsTableName . '.resource_type = ?', "album_photo")
			            ->where($tagmapsTableName . '.tag_type = ?', "user")
			            ->group($tagmapsTableName . ".tag_id")
			            ->order('rand()');
    return $userTable->fetchAll($select);
  }
  public function getFreindTaggedPhoto($fids, $limit = 5, $page = 1, $photo_ignore = 0){
		$photoTable = Engine_Api::_()->getDbtable('Photos', 'Album');
	    $photoTableName = $photoTable->info('name');
	    $tagmapsTable = Engine_Api::_()->getDbtable('TagMaps', 'Core');
	    $tagmapsTableName = $tagmapsTable->info('name');

	    $select = $photoTable->select()
    					->setIntegrityCheck(false)
                    	->from($photoTableName, array())
                    	->join($tagmapsTableName, $tagmapsTableName . '.resource_id=' . $photoTableName . '.photo_id', array($photoTableName.'.*', $tagmapsTableName.'.tag_id'))
  						->where($tagmapsTableName . '.resource_type = ?', "album_photo")
  						->where($tagmapsTableName . '.tag_id in (?)', new Zend_Db_Expr("'" . join("', '", $fids) . "'"))
  						->where($tagmapsTableName . '.tag_type = ?', 'user')
  						->where($tagmapsTableName . '.resource_id <> ?', $photo_ignore)
  						->order($tagmapsTableName. '.creation_date DESC');
//  						->limit($limit, ($page-1)*$limit);
//  		return $photoTable->fetchAll($select);
		return Zend_Paginator::factory($select);
  }
    public function getFreindPhotos($fids){
		$photoTable = Engine_Api::_()->getDbtable('Photos', 'Album');
	    $photoTableName = $photoTable->info('name');
	    $albumTable = Engine_Api::_()->getDbtable('Albums', 'Album');
   		$albumTableName = $albumTable->info('name');

	    $select = $photoTable->select()
    					//->setIntegrityCheck(false)
                    	->from($photoTableName)
                    	->join($albumTableName, $albumTableName . '.album_id=' . $photoTableName . '.album_id', array())
  						->where($albumTableName . '.search = 1')
  						->where($albumTableName . '.owner_id in (?)', new Zend_Db_Expr("'" . join("', '", $fids) . "'"))
//  						->group($photoTableName . '.album_id')
//  						->order($photoTableName. '.creation_date DESC')
						->order('RAND()');
//  						->limit($limit, ($page-1)*$limit);
//  		return $photoTable->fetchAll($select);
//echo $select;die;
			return Zend_Paginator::factory($select);
  }					
}
